package com.egao.common.module.system.transfer;

import cn.hutool.core.util.StrUtil;
import com.egao.common.core.web.JsonResult;
import com.egao.common.module.cms.entity.Article;
import com.egao.common.module.cms.entity.Category;
import com.egao.common.module.cms.service.ArticleService;
import com.egao.common.module.cms.service.CategoryService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringEscapeUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.sql.*;
import java.util.List;

/**
 * 文章数据迁移
 *
 * @author pyx
 */
@Api(tags = "文章数据迁移")
@RestController
@Slf4j
public class ArticleDataController {
    @Value("${old.datasource.jyurl}")
    private String url;

    @Value("${old.datasource.username}")
    private String userName;

    @Value("${old.datasource.password}")
    private String password;

    @Value("${minio.old-data-head}")
    private String contentUrl;

    @Autowired
    private ArticleService articleService;
    @Autowired
    private CategoryService categoryService;

    public JsonResult article() {
        Connection conn = null;
        Statement st = null;
        ResultSet rs = null;

        try{
            Class.forName("com.mysql.cj.jdbc.Driver");
            conn = DriverManager.getConnection(url, userName, password);
            st = conn.createStatement();
            String sql = "SELECT * FROM article WHERE del_flag = '0'";
            rs = st.executeQuery(sql);

            while (rs.next()){
                Article article = new Article();
                article.setOldId(rs.getString("id"));
                article.setOldCategoryId(rs.getString("articlecategoryid"));
                article.setTitle(StringEscapeUtils.unescapeHtml4(rs.getString("title")));
                String comment = rs.getString("comment");
                article.setContent(comment != null ? comment.replaceAll("/ueditor/jsp/upload", contentUrl + "/ueditor/jsp/upload") : "");
                article.setHits(rs.getInt("hit"));
                article.setSort(rs.getInt("sort"));
                article.setCreateTime(rs.getDate("create_date"));
                article.setUpdateTime(rs.getDate("update_date"));
                int istop = 0;
                istop = rs.getInt("istop");
                String top = null;
                if(istop == 0){
                    top = "0";
                }else{
                    top = "1";
                }
                article.setTopFlag(top);

                //封面地址处理
                String imgUrl = rs.getString("imageurl");
                if(!StrUtil.isBlank(imgUrl)){
                    //防止重复更新
                    if(imgUrl.indexOf("http://192.168.1.5:9000") == -1){
                        imgUrl = imgUrl.replace("/upload","http://192.168.1.5:9000/zuel/old/files/upload");
                    }
                }
                article.setImgUrl(imgUrl);

                try {
                    articleService.save(article);
                }catch (Exception e){
                    log.error("插入id为: {} 的数据出现异常",rs.getString("id"));
                    break;
                }
            }

            matching();

        }catch (ClassNotFoundException | SQLException e){
            e.printStackTrace();
        }finally {
            try {
                rs.close();
                st.close();
                conn.close();
                return JsonResult.ok();
            }catch (SQLException e){
                e.printStackTrace();
                return JsonResult.error();
            }
        }
    }

    public void matching(){
        List<Category> categoryList = categoryService.oldCategoryList();
        List<Article> articleList = articleService.oldArticleList();

        for (Article article:articleList) {
            if(!StrUtil.isBlank(article.getOldCategoryId())){
                for (Category category: categoryList) {
                    if(article.getOldCategoryId().equals(category.getOldId())){
                        article.setCategoryId(category.getId());
                    }
                }
            }
        }

        articleService.updateBatchById(articleList);
    }

}
